﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gmis.IDAL;
using System.Data;
using System.Data.SqlClient;
using Gmis.DBUtility;
namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:News
    /// </summary>
    public class News : INews
    {
        public News()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Gmis.Model.News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into News(");
            strSql.Append("sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,lEnable,sVillage,sComment,dCreate)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sColumn,@sType,@sTitle,@sContent,@sAuthor,@dDate,@sCreator,@lEnable,@sVillage,@sComment,@dCreate)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sColumn", SqlDbType.VarChar,50),
					new SqlParameter("@sType", SqlDbType.VarChar,50),
					new SqlParameter("@sTitle", SqlDbType.NVarChar,300),
					new SqlParameter("@sContent", SqlDbType.NText),
					new SqlParameter("@sAuthor", SqlDbType.NText),
					new SqlParameter("@dDate", SqlDbType.DateTime),
					new SqlParameter("@sCreator", SqlDbType.VarChar,50),
					new SqlParameter("@lEnable", SqlDbType.Bit,1),
					new SqlParameter("@sVillage", SqlDbType.VarChar,50),
					new SqlParameter("@sComment", SqlDbType.NVarChar,300),
					new SqlParameter("@dCreate", SqlDbType.DateTime)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sColumn;
            parameters[2].Value = model.sType;
            parameters[3].Value = model.sTitle;
            parameters[4].Value = model.sContent;
            parameters[5].Value = model.sAuthor;
            parameters[6].Value = model.dDate;
            parameters[7].Value = model.sCreator;
            parameters[8].Value = model.lEnable;
            parameters[9].Value = model.sVillage;
            parameters[10].Value = model.sComment;
            parameters[11].Value = model.dCreate;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update News set ");
            //strSql.Append("sColumn=@sColumn,");
            //strSql.Append("sType=@sType,");
            strSql.Append("sTitle=@sTitle,");
            strSql.Append("sContent=@sContent,");
            //strSql.Append("sAuthor=@sAuthor,");
            //strSql.Append("dDate=@dDate,");
            //strSql.Append("sCreator=@sCreator,");
            strSql.Append("lEnable=@lEnable");
            //strSql.Append("sVillage=@sVillage,");
            //strSql.Append("sComment=@sComment,");
            //strSql.Append("dCreate=@dCreate");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					//new SqlParameter("@sColumn", SqlDbType.VarChar,50),
					//new SqlParameter("@sType", SqlDbType.VarChar,50),
					new SqlParameter("@sTitle", SqlDbType.NVarChar,300),
					new SqlParameter("@sContent", SqlDbType.NText),
					//new SqlParameter("@sAuthor", SqlDbType.NText),
					//new SqlParameter("@dDate", SqlDbType.DateTime),
					//new SqlParameter("@sCreator", SqlDbType.VarChar,50),
					new SqlParameter("@lEnable", SqlDbType.Bit,1)
					//new SqlParameter("@sVillage", SqlDbType.VarChar,50),
					//new SqlParameter("@sComment", SqlDbType.NVarChar,300),
					//new SqlParameter("@dCreate", SqlDbType.DateTime)
                                        };
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sTitle;
            parameters[2].Value = model.sContent;
            parameters[3].Value = model.lEnable;
            //parameters[1].Value = model.sColumn;
            //parameters[2].Value = model.sType;
            //parameters[3].Value = model.sTitle;
            //parameters[4].Value = model.sContent;
            //parameters[5].Value = model.sAuthor;
            //parameters[6].Value = model.dDate;
            //parameters[7].Value = model.sCreator;
            //parameters[8].Value = model.lEnable;
            //parameters[9].Value = model.sVillage;
            //parameters[10].Value = model.sComment;
            //parameters[11].Value = model.dCreate;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from News ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from News ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.News GetModel(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  top 1 n.sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,n.lEnable,n.sVillage,n.sComment,n.dCreate ,c.sName as columnName,v.sName as villageName ");
            strSql.Append(" from News n inner join  [Gmis].[dbo].[Column] c on n.sColumn=c.sid");
            strSql.Append(" inner join [Gmis].[dbo].[Village] v on c.SVillage=v.sId ");
            strSql.Append(" where n.sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.News model = new Gmis.Model.News();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                model.sColumn = ds.Tables[0].Rows[0]["sColumn"].ToString();
                model.sType = ds.Tables[0].Rows[0]["sType"].ToString();
                model.sTitle = ds.Tables[0].Rows[0]["sTitle"].ToString();
                model.sContent = ds.Tables[0].Rows[0]["sContent"].ToString();
                model.sAuthor = ds.Tables[0].Rows[0]["sAuthor"].ToString();
                if (ds.Tables[0].Rows[0]["dDate"].ToString() != "")
                {
                    model.dDate = DateTime.Parse(ds.Tables[0].Rows[0]["dDate"].ToString());
                }
                model.sCreator = ds.Tables[0].Rows[0]["sCreator"].ToString();
                if (ds.Tables[0].Rows[0]["lEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["lEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["lEnable"].ToString().ToLower() == "true"))
                    {
                        model.lEnable = true;
                    }
                    else
                    {
                        model.lEnable = false;
                    }
                }
                model.sVillage = ds.Tables[0].Rows[0]["sVillage"].ToString();
                model.sComment = ds.Tables[0].Rows[0]["sComment"].ToString();
                if (ds.Tables[0].Rows[0]["dCreate"].ToString() != "")
                {
                    model.dCreate = DateTime.Parse(ds.Tables[0].Rows[0]["dCreate"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,lEnable,sVillage,sComment,dCreate ");
            //strSql.Append(" FROM News ");
            strSql.Append(" select   n.sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,n.lEnable,n.sVillage,n.sComment,n.dCreate ,c.sName as columnName,v.sName as villageName ");
            strSql.Append(" from News n inner join  [Gmis].[dbo].[Column] c on n.sColumn=c.sid");
            strSql.Append(" inner join [Gmis].[dbo].[Village] v on c.SVillage=v.sId ");
            //strSql.Append(" where n.sId=@sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            //strSql.Append(" sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,lEnable,sVillage,sComment,dCreate ");
            //strSql.Append(" FROM News ");
            strSql.Append(" n.sId,sColumn,sType,sTitle,sContent,sAuthor,dDate,sCreator,n.lEnable,n.sVillage,n.sComment,n.dCreate ,c.sName as columnName,v.sName as villageName ");
            strSql.Append(" from News n inner join  [Gmis].[dbo].[Column] c on n.sColumn=c.sid");
            strSql.Append(" inner join [Gmis].[dbo].[Village] v on c.SVillage=v.sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "News";
            parameters[1].Value = "";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
